/*
This file is part of the replication package of "Asset-Price Redistribution" by
Fagereng, Gomez, Gouin-Bonenfant, Holm, Moll, and Natvik. 

This file produces a set of summary statistics by year and cohort.   

*/


********************************************************************************
use nt* housing housing_pb debt debt_pb deposits deposits_pb stocks stocks_pb mfund bonds equity_pb net_private_debt stocks_nonreg origSample lnr year death spending *_wealth ltv_debt b_year cons_* using $data/apr_9319, clear

* WITHIN COHORT WEALTH
sort year b_year total_wealth
bys year b_year (total_wealth): gen _qqq = _n/_N
qui gen _percentile = ceil(_qqq*100)-1


cap drop _temp	
gen _temp = .
replace _temp = _percentile if year == 1993
bys lnr: egen percentile = max(_temp)
drop _temp

cap drop _*

* both nt and level
global namelist housing housing_pb stocks stocks_pb mfund equity_pb

* only level
global namelist2 debt debt_pb deposits deposits_pb bonds net_private_debt stocks_nonreg

********************************************************************************
* MAKE SHEETS
********************************************************************************

** statistics by year and cohort
preserve
drop if b_year == . 
foreach var in $namelist $namelist2 total_wealth {
	*bys year b_year: egen `var'_mean = mean(`var')
	bys year b_year: egen `var'_sum = total(`var')
}	
foreach var in $namelist {
	*bys year b_year: egen nt_`var'_mean = mean(nt_`var')
	bys year b_year: egen nt_`var'_sum = total(nt_`var')
	}
bys year b_year: gen num_mean = _N

* ensure that we do not report summary stats in too small groups
drop if num_mean <= 10

bys year b_year: keep if _n == 1
keep year b_year *_mean *_sum
export delimited ${output}/year_cohort.csv, replace
restore



** statistics by year, cohort, and percentile (of total wealth within cohort)
preserve

* drop 8 observations that are extreme outliers in some specific percentiles
drop if lnr == 75317493402
drop if lnr == 6109920003
drop if lnr == 63205298574
drop if lnr == 24088016277
drop if lnr == 9239245143
drop if lnr == 69176808420
drop if lnr == 6080171325
drop if lnr == 36318741978

foreach var in $namelist $namelist2 total_wealth {
	*bys year b_year percentile: egen `var'_mean = mean(`var')
	bys year b_year percentile: egen `var'_sum = total(`var')
}	
foreach var in $namelist {
	*bys year b_year percentile: egen nt_`var'_mean = mean(nt_`var')
	bys year b_year percentile: egen nt_`var'_sum = total(nt_`var')
}
bys year b_year percentile: gen num_mean = _N

* ensure that we do not report summary stats in too small groups
drop if num_mean <= 10

bys year b_year percentile: keep if _n == 1
keep year b_year percentile *_mean *_sum
export delimited ${output}/year_cohort_percentile.csv, replace
restore
